package cn.hxh.page.service.impl;

import cn.hxh.page.mapper.MemoMapper;
import cn.hxh.page.pojo.Memo;
import cn.hxh.page.service.MemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author: HongXiaoHan
 * @Description
 * @Date: 2020/10/6 20:25
 */
@Component
public class MemoServiceImpl implements MemoService {

    @Autowired
    private MemoMapper memoMapper;

    @Override
    public void memoInsert(String content, String id) {
        Memo memo = new Memo();
        memo.setUpdateTime(new Date());
        memo.setCreateTime(new Date());
        memo.setContent(content);
        memo.setUserId(id);
        memoMapper.insertMemo(memo);
    }

    @Override
    public List<Memo> selectMemoAll(String userId) {

        return memoMapper.selectMemoAll(userId);
    }

    @Override
    public void delete(String id, String content, Date time) {
        memoMapper.delete(id, content, time);
    }

    @Override
    public int top(String id, String content, Date date) {
        if (memoMapper.flagTop(id, content, date) == 1) {
            //已经置顶
            return 3;
        }
        int i = memoMapper.count(id);
        if (i >= 4) {
            //最多四条
            return 2;
        } else {
            //置顶成功
            memoMapper.top(id, content, date);
            return 1;
        }
    }

    @Override
    public List<Memo> selectTop(String id) {
        return memoMapper.selectTop(id);
    }

    @Override
    public int noTop(String id, String content, Date date) {
        int flag;
        memoMapper.noTop(id, content, date);
        flag = 1;
        return flag;
    }
}
